<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*-----------------------------------------------------------------------------------------------
    Get_contact_form_block()       
    -----------------------------------------------------------------------------------------------*/
class Get_cf_block extends Front_Controller{

    function Get_contact_form_block(){
        parent::Front_Controller();
    }
    
    function index(){
        $CI =&get_instance();

        $this->view(get_class($this));
    }
	
	function submit_form(){
		$CI =&get_instance();
	}
	function send_mail(){
        $this->view('auto_complete');
        //Send a email to client
		$CI = & get_instance();				
		$booking_id = $CI->uri->segment(3);
		$CI->booking_model->set_id($booking_id);
		if(!$CI->booking_model->get()){
			echo 'This booking not found.';
			return;
		}
		$order_rate = $CI->booking_model->order_rate_list($booking_id);
		$rate_info= "";
		$user = $CI->auth->get_auth_data();

		$CI->load->module_library('front.email_templates.email_parser');
		$CI->email_parser->template('comfirm_addnew_booking');
		//$CI->email_parser->from($user['email']);
		$arr_receive = array();
		if($CI->booking_model->{ORDER_CUST_EMAIL})
			$arr_receive[] = $CI->booking_model->{ORDER_CUST_EMAIL};
		if($CI->booking_model->{ORDER_PER_EMAIL})
			$arr_receive[] = $CI->booking_model->{ORDER_PER_EMAIL};
			
		$CI->email_parser->to($arr_receive);
		$CI->email_parser->add('booking_number',$CI->booking_model->{ORDER_ORDER_NUMBER});
		$CI->email_parser->add('from_customer',$CI->booking_model->{ORDER_FROM_NAME});
		$CI->email_parser->add('to_customer',$CI->booking_model->{ORDER_TO_NAME});
		$CI->email_parser->add('reference',$CI->booking_model->{ORDER_CUSTOMER_REF});
        $html = '<table width="100%" border="1" style="border-collapse: collapse">';
        $html .= '<tr><th width="20%">Quantity</th><th width="20%">Item</th><th>Description</th></tr>';
        $row = '<tr><td align="center">%s</td><td align="center">%s</td><td>%s</td></tr>';
        foreach ( $order_rate as $o_rate){
			//$rate_info .= $o_rate->{ORDER_RATE_QUANTITY}." x ".$o_rate->{ORDER_RATE_ITEM}."<br />";
			$html .= sprintf($row, $o_rate->{ORDER_RATE_QUANTITY}, $o_rate->{ORDER_RATE_ITEM}, $o_rate->{ORDER_RATE_DESCRIPTION});
		}
		$html .= '</table>';
		$CI->email_parser->add('rate_info',$html);
		
		$CI->email_parser->add('client_name',$CI->booking_model->{ORDER_CUST_NAME});
		$CI->email_parser->add('user_name',$user['fullname']);
		$CI->email_parser->add('user_role',$user['role_name']);
		if(count($arr_receive)>0){
			$CI->email_parser->send();
			//Insert customer note
			$CI->load->module_model('customers.customer_note_model');
            $new_note = array(CUSTOMER_NOTE_TYPE => NOTE_TYPE_NOTE,
                              CUSTOMER_NOTE_CUSTOMER_ID => $CI->booking_model->{ORDER_CUSTOMER_ID},
                              CUSTOMER_NOTE_NOTES => "Booking ".$CI->booking_model->{ORDER_ORDER_NUMBER}." sent to client.",
                              CUSTOMER_NOTE_CREATE_DATE => now_to_mysql(),
                              CUSTOMER_NOTE_UPDATE_DATE => now_to_mysql(),
                              CUSTOMER_NOTE_DELETE_FLAG => 0,
                              CUSTOMER_NOTE_USER_ID => $user['user_id'],
                              );
            $CI->customer_note_model->save($new_note);
			echo '<success>1</success>';
		}
		else{
			echo 'There is error send mail.';
		}		
	}

}